package common;

import java.io.*;

public class FileUtil {
    /**
     * 负责读取文件中的内容返回一个字符串
     * @param filePath
     * @return
     */
    public static String readFile(String filePath){
        StringBuilder sb = new StringBuilder();
        try(FileReader fileReader = new FileReader(filePath)) {
            while(true){
                int ch = fileReader.read();
                if(ch == -1){
                    break;
                }
                sb.append((char)ch);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将字符串中的内容写入一个文件中
     * @param filePath
     * @param context
     */
    public static void writeFile(String filePath,String context){
        try(FileWriter fileWriter = new FileWriter(filePath)){
            fileWriter.write(context);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
